import { ContactModel } from './ContactModel'

/** 人事事务 */
export enum JobAction {
  /** 空状态 */
  None,
  /** 任职 */
  Start,
  /** 请假 */
  Pause,
  /** 离职 */
  End,
  /** 调整任期 */
  Schedule,
  /** 奖金 */
  Bonus,
  /** 预支 */
  Advance,
  /** 结清 */
  Settlement,
  /** 记过 */
  Blame,
}

// const nameOfAction = new Map<JobAction, string>([
//   [JobAction.None, '无'],
//   [JobAction.None, '无'],
//   [JobAction.None, '无'],
//   [JobAction.None, '无'],
//   [JobAction.None, '无'],
//   [JobAction.None, '无'],
// ])

export class JobRecordModel {
  id: number
  worker?: ContactModel
  date: Date
  action: JobAction
  actionParams: any[]

  constructor() {
    this.id = 0
    this.worker = undefined
    this.date = new Date()
    this.action = JobAction.None
    this.actionParams = []
  }

  static actionName(act: JobAction) {
    const obj = new JobRecordModel()
    obj.action = act
    return obj.actionName()
  }

  actionName() {
    if (this.action === JobAction.None) {
      return '无(未选择)'
    }
    else if (this.action === JobAction.Start) {
      return '入职'
    }
    else if (this.action === JobAction.Schedule) {
      return '更改排期'
    }
    else if (this.action === JobAction.Pause) {
      return '请假'
    }
    else if (this.action === JobAction.End) {
      return '离职'
    }
    else if (this.action === JobAction.Bonus) {
      return '奖金'
    }
    else if (this.action === JobAction.Advance) {
      return '预支'
    }
    else if (this.action === JobAction.Settlement) {
      return '结清'
    }
    else if (this.action === JobAction.Blame) {
      return '记过'
    }
    else {
      return '(错误❌)'
    }
  }

  description() {
    if (this.action === JobAction.None) {
      return '无(未选择)'
    }
    else if (this.action === JobAction.Start) {
      return `入职(工资${this.actionParams[0]}, ${this.actionParams[1]})`
    }
    else if (this.action === JobAction.Schedule) {
      return `更改排期到(${this.actionParams[0]})`
    }
    else if (this.action === JobAction.Pause) {
      const reason = this.actionParams[1] || '无原因'
      return `请假(${this.actionParams[0]}天, ${reason})`
    }
    else if (this.action === JobAction.End) {
      const reason = this.actionParams[0] || '无原因'
      return `离职(${reason})`
    }
    else if (this.action === JobAction.Bonus) {
      const reason = this.actionParams[1] || '无原因'
      return `奖金(${this.actionParams[0]}元, ${reason})`
    }
    else if (this.action === JobAction.Advance) {
      const reason = this.actionParams[1] || '无原因'
      return `预支(${this.actionParams[0]}元, ${reason})`
    }
    else if (this.action === JobAction.Settlement) {
      return '工资结清'
    }
    else if (this.action === JobAction.Blame) {
      const reason = this.actionParams[1] || '无原因'
      return `记过(罚金: ${this.actionParams[0]}元, ${reason})`
    }
    else {
      return '(错误❌)'
    }
  }

  static sort(arr: JobRecordModel[], dsc = false) {
    arr.sort((a, b) => {
      const _d = (x: JobRecordModel) => {
        // 为了避免入职，离职，结清等事件难以排序。分别对其进行一定程度的处理
        if (x.action === JobAction.End) {
          return x.date.valueOf() + 10000
        }
        else if (x.action === JobAction.Settlement) {
          return x.date.valueOf() + 20000
        }
        else {
          return x.date.valueOf()
        }
      }
      if (dsc) {
        return _d(b) - _d(a)
      }
      else {
        return _d(a) - _d(b)
      }
    })
  }
}
